﻿<!--#INCLUDE file="conns/CONN_BLOG.asp" -->
<%
'---------------------------------------------------------------------
'数据库连接模块                                                         
'
'author: evan zhao                                                    
'---------------------------------------------------------------------
    
    '常量MISSLOG_ROOT设置已经移至config.asp，请到config.asp中修改

    '全局变量，各包含conn.asp的文件均可直接使用，调用后请勿释放conn,userConn和rs对象，整个请求响应执行结束时会自动释放。
	dim conn, userConn, rs, sql, db
	
	dim blogDB  'blog数据库路径

	class DBManager
		Private Sub Class_Initialize() 
			'如果blog数据库在其他位置请更改数据库路径
			blogDB = Server.MapPath(MISSLOG_ROOT & "/db/blog_db.asp")
			
			'response.write blogDB
			'response.end
		    
		    if isEmpty(userDB) then
		        userDB = blogDB
		    end if
		End Sub
		
		Private Sub Class_Terminate() 
			call CloseDB()
		End Sub
			
		Public Sub OpenDB
		  dim connstr
		  
		  'on error resume next
		  
	      'response.write "before openDB cost: " & (timer() - curtime) &"<br>"
	      'curtime = timer()
		  if not isObject(conn) then
	    	Set conn = Server.CreateObject("ADODB.Connection")
	    	connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & blogDB
	    	'connstr="driver={Microsoft Access Driver (*.mdb)};dbq=" & blogDB
	    	conn.Open connstr
	    	
	    	if userDB=blogDB then
	    	    userDB = blogDB
	    	    set userConn = conn
	    	else    
	        	Set userConn = Server.CreateObject("ADODB.Connection")
	        	connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & userDB
	        	userConn.Open connstr
	    	end if
	    	
	      end if
	      
	      if Err.number<>0 then
	    	message("<p align=left>打开数据库失败！错误描述：<br>" & Err.Description & "<br>可能服务器不稳定或数据库文件损坏或者系统参数(config.asp中MISSLOG_ROOT)设置错误。</p>")
	    	response.end
	  	  end if
	      on error goto 0
	      
	      if not isObject(rs)  then
	    	set rs = Server.CreateObject("ADODB.Recordset")
	    	set rs.ActiveConnection = conn
	      end if
	      
	      'response.write "openDB cost: " & (timer() - curtime) &"<br>"
	      'curtime = timer()
	      
		end sub
		
		
	    public Sub CloseDB
	
	          on error resume next
	
	          rs.close()
	          set rs = nothing
	          rs = Empty
	
	          conn.close()
	          set conn = Nothing
	          conn = Empty
	          
	          userConn.close()
	          set userConn = Nothing
	          userConn = Empty
	    	  
	    	  on error goto 0
	    End Sub
	    		
	end class
	
	Set db = new DBManager
	
	Sub OpenDB
		call db.OpenDB()
	End Sub
	
	Sub CloseDB
		call db.CloseDB()
	End Sub
    
%>